package com.zkzx.company.api.controller.file;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.jcraft.jsch.SftpException;
import com.zkzx.common.constants.FileUploadEnum;
import com.zkzx.util.date.DateUtil;
import com.zkzx.util.ftp.FtpClient;
import com.zkzx.util.ftp.utils.FtpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
 * @author young Email:yanghui@zkraising.com
 * @description
 * @company 北京中科智兴物联科技有限公司 @版权所有
 * @since 2021年05月06日 10时18分
 */
@RestController
@Validated
@Api(tags="文件管理")
@RequestMapping(value = "/files")
public class FileController {

    @Autowired
    private FtpUtil ftpUtil;

    @NacosValue(value = "${ftpReadFilePath:/upload}", autoRefreshed = true)
    private String path;

    @NacosValue(value = "${ftpExtranet}", autoRefreshed = true)
    private String ftpExtranet;

    @PostMapping("/upload")
    @ResponseStatus(HttpStatus.CREATED)
    @ApiOperation(value = "新增文件", notes = "新增文件")
    public String upload(@ApiParam(name = "file", value = "文件",required = true)@RequestParam("file")  MultipartFile file,
                         @ApiParam(name = "type", value = "类型：COMPANY=企业;DRIVER=司机;TRUCK=货车",required = true)
                         @RequestParam("type") FileUploadEnum type) throws IOException, SftpException {
        FtpClient ftpClient = ftpUtil.getFtpClient();
        String targetFilePath = path.concat(type.getPath())
                .concat(DateUtil.convertDateToString(DateUtil.SHORT_DATE_FOR_MATER,new Date()));
        ftpClient.send(file.getInputStream(),targetFilePath,file.getOriginalFilename());
        return ftpExtranet.concat(targetFilePath).concat("/")
                .concat(file.getOriginalFilename());
    }



}
